package org.budo.support.java.concurrent.executor.factory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import org.budo.support.java.concurrent.thread.factory.BudoThreadFactory;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class ExecutorServiceFactory {
    private static final Logger log = Slf4j.getLogger();

    private static final ExecutorServiceFactory _INSTANCE = new ExecutorServiceFactory();

    private Integer threadCount = 0;

    public static ExecutorServiceFactory getInstance() {
        return _INSTANCE;
    }

    public ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        synchronized (ExecutorServiceFactory.class) {
            this.threadCount += nThreads;
        }

        log.warn("#30 newFixedThreadPool, nThreads=" + nThreads + ", threadCount=" + threadCount);

        return Executors.newFixedThreadPool(nThreads, threadFactory);
    }

    public ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return Executors.newSingleThreadExecutor(threadFactory);
    }

    public ExecutorService newFixedThreadPool(int nThreads) {
        BudoThreadFactory threadFactory = new BudoThreadFactory();
        return this.newFixedThreadPool(nThreads, threadFactory);
    }
}